package com.example.userp.app.myapplication.RcycleView.DividerItem;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * Describe:
 * Created by ysy on 2018/9/19.
 */
public class BaseDividerItemDecoration1 extends RecyclerView.ItemDecoration {
    private static String TAG = "BaseDividerItemDecoration";
    private final Paint mPaint;

    private int mDividerColor = Color.GREEN;
    private int mDividerStrokeWidth = 10;
    private boolean isShowFirstDivider = false;
    private boolean isShowLastDivider = false;
    private boolean isShowRightDivider = false;
    private boolean isShowLeftDivider = false;
    private int mRightMargin = 0;
    private int mLeftMargin = 0;

    public BaseDividerItemDecoration1() {
        mPaint = new Paint();
        mPaint.setColor(mDividerColor);
        mPaint.setStrokeWidth(mDividerStrokeWidth);
        mPaint.setAntiAlias(true);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.Adapter adapter = parent.getAdapter();
        int itemCount = adapter.getItemCount();
        int childAdapterPosition = parent.getChildAdapterPosition(view);

        if (isLayoutManager(parent) == 0) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
            int spanCount = gridLayoutManager.getSpanCount();
            if (childAdapterPosition <= spanCount - 1 && isShowFirstDivider) {//限制第行顶部
                outRect.top = mDividerStrokeWidth;
            }

            int remaining = itemCount - childAdapterPosition;
            int mRemainder = itemCount % spanCount;

            if ((mRemainder == 0 && remaining > spanCount) && remaining > mRemainder) {
                outRect.bottom = mDividerStrokeWidth;
            } else {
                if (isShowLastDivider) {//限制最后一行
                    outRect.bottom = mDividerStrokeWidth;
                }
            }

            if (childAdapterPosition % spanCount == 0 && isShowLeftDivider) {//最左边
                outRect.left = mDividerStrokeWidth;
            }

            if (childAdapterPosition % spanCount != spanCount - 1) {//最右边
                outRect.right = mDividerStrokeWidth;
            } else {
                if (isShowRightDivider) {
                    outRect.right = mDividerStrokeWidth;
                }
            }
        }
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        RecyclerView.Adapter adapter = parent.getAdapter();
        int itemCount = adapter.getItemCount();
        int childCount = parent.getChildCount();
        int height = parent.getHeight();
        int width = parent.getWidth();

        for (int i = 0; i < childCount; i++) {
            View childAt = parent.getChildAt(i);
            int childAdapterPosition = parent.getChildAdapterPosition(childAt);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childAt.getLayoutParams();

            //行 线
            int v_left = parent.getPaddingLeft() + layoutParams.leftMargin + mLeftMargin;
            int v_top = childAt.getTop() - mDividerStrokeWidth;
            int v_right = width - parent.getPaddingRight() - layoutParams.rightMargin - mRightMargin;
            int v_bottom = childAt.getTop();

            if (v_top >= parent.getPaddingTop() - mDividerStrokeWidth && v_bottom <= height - parent.getPaddingBottom()) {
                c.drawRect(v_left, v_top, v_right, v_bottom, mPaint);
                if (childAdapterPosition == itemCount - 1 && isShowLastDivider && childAt.getBottom() <= height - parent.getPaddingBottom()) {
                    c.drawRect(v_left, childAt.getBottom(), v_right, childAt.getBottom() + mDividerStrokeWidth, mPaint);
                }
            }

            //竖 线
            final int h_top = parent.getPaddingTop();
            final int h_bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
            final int h_left = childAt.getRight() + layoutParams.rightMargin;
            final int h_right = h_left + mDividerStrokeWidth;

            if (h_left >= childAt.getRight() + layoutParams.rightMargin - mDividerStrokeWidth && h_right < width + parent.getPaddingRight()) {
                c.drawRect(h_left, h_top, h_right, h_bottom, mPaint);

                if (childAdapterPosition == 0 && isShowLeftDivider) {
                    c.drawRect(childAt.getLeft() - mDividerStrokeWidth, h_top, childAt.getLeft(), h_bottom, mPaint);
                }
            }
        }

    }

    /**
     * 判断LayoutManager的类型
     */
    private int isLayoutManager(RecyclerView recyclerView) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            return 0;
        } else if (layoutManager instanceof LinearLayoutManager) {
            return 1;
        }
        return 2;
    }
}
